<?php

/**
 * Created by phpstorem.
 * User: yangakw@qq.com
 * Sign: 猥琐发育，别浪
 * Date: 2018-06-23
 * Time: 上午 9:59
 */
class Userfee extends CI_Model
{
    public function by_id($id)
    {
        return $this->db->from('user_fee')->where("id",$id)->get()->row();
    }
    public function by_user_id($user_id)
    {
        BEGIN:
        $data = $this->db->from('user_fee')->where("user_id",$user_id)->get()->row();
        if(empty($data)){
            $this->db->insert('user_fee',[
                "user_id"=>$user_id,
                "fee"=>0
            ]);
            goto BEGIN;
        }
        return $data;
    }
    public function change($user_id,$amt,$memo){
        $feeobj = $this->by_user_id($user_id);
        $fee = $feeobj->fee;
        if( empty($amt) ){
            return [
                "code"=>1004,
                'msg'=>"余额变动为0"
            ];
        }
        if($fee+$amt < 0 ){
            return [
                "code"=>1003,
                'msg'=>"余额不足"
            ];
        }
        $fee = $fee+$amt;
        $this->load->model('Userfeedetail');
        $Userfeedetail = new Userfeedetail();
        $Userfeedetail->add_($user_id,$amt,$memo);
        $this->db->where("user_id",$user_id)->update("user_fee",[
            "fee"=>$fee
        ]);
    }
}